package com.lms.demo.fileservice.controller;

import com.lms.demo.autoconfigure.minio.MinioTemplate;
import com.lms.demo.fileservice.annotation.Log;
import com.lms.demo.fileservice.service.FileService;
import com.lms.demo.fileservice.util.ResponseResult;
import io.minio.errors.MinioException;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

/**
 * @Author: 李孟帅
 * @CreateTime: 2020-12-24 11:44
 * @Description:
 */
@RestController
public class FileController {
    private static final Logger LOGGER = LoggerFactory.getLogger(FileController.class);

    @Autowired
    private FileService fileService;

    @Autowired
    private MinioTemplate minioTemplate;

    @PostMapping("/upload")
    @Log("文件上传")
    public ResponseResult<String> upload(@RequestPart("file") MultipartFile file) throws Exception {
        String fileId = fileService.upload(file);
        String property = System.getProperty("java.io.tmpdir");
        System.out.println(property);
        return ResponseResult.success(fileId);
    }

    @PostMapping("/upload2")
    @Log("文件上传")
    public ResponseResult<String> upload2(HttpServletRequest request) throws Exception {
        InputStream inputStream = request.getInputStream();
        System.out.println("------");
        byte[] bytes = new byte[1024];
        int len;
        while ((len = inputStream.read(bytes)) != -1) {
            System.out.println(new String(bytes, "GBK"));
        }
        inputStream.close();
        return ResponseResult.success();
    }

    @RequestMapping("/download")
    @Log("文件下载")
    public void download(String file, HttpServletResponse response) throws
            MinioException, NoSuchAlgorithmException, InvalidKeyException, IOException {
        String objectName = "Wireshark-win64-3.2.3.exe";
        InputStream inputStream = minioTemplate.downloadByObjectName("filetest", file);
        response.setHeader("Content-type", "application/octet-stream");
//        response.setContentType("application/x-zip");
        response.setHeader("Content-disposition", "attachment;fileName=" + file);
        OutputStream outputStream = new BufferedOutputStream(response.getOutputStream());
        byte[] bytes = new byte[1024];
        int index;
//        index = inputStream.read(bytes);
        while ((index = inputStream.read(bytes)) != -1) {
            outputStream.write(bytes, 0, index);
        }
        outputStream.flush();
        outputStream.close();
    }

}
